package com.gee.spring.boot.oauth2.auth.service.impl;

import com.gee.spring.boot.oauth2.auth.entity.AuthUser;
import com.gee.spring.boot.oauth2.auth.entity.User;
import com.gee.spring.boot.oauth2.auth.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.Set;

/**
 * desc:
 *
 * @author gee
 * @date 2020/03/22 16:14
 */
@Service
public class GeeUserDetailServiceImpl implements UserDetailsService {

    @Autowired
    private UserService userService;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        User user = userService.getByUserName(username);
        if (user == null){
            return null;
        }
        Set<GrantedAuthority> authorities = new HashSet<>();
        authorities.add(new SimpleGrantedAuthority("1"));
        return new AuthUser(user.getId(),user.getUsername(),user.getPassword(),authorities);
    }

}
